package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.api.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.mapper.StrategyCatalogMapper;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.business.vo.CatalogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 攻略分类Service业务层处理
 *
 * @author 沙琪玛
 * @date 2024-11-01
 */
@Service
@Transactional
public class StrategyCatalogServiceImpl extends ServiceImpl<StrategyCatalogMapper, StrategyCatalog> implements IStrategyCatalogService {
    @Autowired
    StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyCatalog> queryPage(StrategyCatalogQuery qo) {
        IPage<StrategyCatalog> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<Strategy> getGroupList() {
//        QueryWrapper<Strategy> QueryWrapper = new QueryWrapper<>();
//        QueryWrapper.select("dest_id,dest_name").groupBy("dest_name");
        LambdaQueryWrapper<Strategy> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.select(Strategy::getDestId, Strategy::getDestName).groupBy(Strategy::getDestName);
        List<Strategy> strategies = strategyMapper.selectList(QueryWrapper);
        strategies.forEach(System.out::println);
        for (Strategy strategy : strategies) {
            List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestId, strategy.getDestId()).list();
            list.forEach(System.out::println);
            strategy.setCatalogList(list);
        }
        return strategies;
    }

    @Override
    public List<CatalogVO> QueryCatalogVO() {
        List<StrategyCatalog> distinct_dest_name = query().select("distinct dest_name").list();
        List<CatalogVO> vos = new ArrayList<>();
        for (cn.wolfcode.wolf2w.api.domain.StrategyCatalog catalog : distinct_dest_name) {
            String destName = catalog.getDestName();
            CatalogVO catalogVO = new CatalogVO();
            catalogVO.setDestName(destName);

            List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestName, destName)
                    .select(StrategyCatalog::getId, StrategyCatalog::getName)
                    .list();
            catalogVO.setCatalogList(list);

            vos.add(catalogVO);
        }
        return vos;
    }
}
